/**
 * Created by Tim on 2017/5/23.
 */
$(function () {
    function UserMgr() {
        // 表格ID
        this.dataTable = $("#dataTable");
        // 按钮
        this.$seachr_btn = $('#btn_search');
        this.$btn_search_clear = $('#btn_search_clear');

        // 初始化
        this.init();
    }

    UserMgr.prototype.Constructor = UserMgr;

    UserMgr.prototype.init = function () {
        this.loadTable();
        this.registEvent();
    };

    UserMgr.prototype.loadTable = function () {
        this.dataTable.dataTable({
            "sAjaxSource": "/admins/page",
            "aoColumns": [
                {"sWidth": "10%", "sTitle": "用户名", "sClass": "center", "mData": "username", "bSortable": false},
                {"sWidth": "8%", "sTitle": "主体类型", "sClass": "center", "mData": "type", "bSortable": false},
                {"sWidth": "15%", "sTitle": "主体名称", "sClass": "center", "mData": "subjectName", "bSortable": false},
                {"sWidth": "12%", "sTitle": "姓名", "sClass": "center", "mData": "realName", "bSortable": false},
                {"sWidth": "8%", "sTitle": "性别", "sClass": "center", "mData": "gender", "bSortable": false},
                {"sWidth": "12%", "sTitle": "手机号码", "sClass": "center", "mData": "mobile", "bSortable": false},
                {"sWidth": "15%", "sTitle": "创建时间", "sClass": "center", "mData": "created", "bSortable": false},
                {"sWidth": "8%", "sTitle": "状态", "sClass": "center", "mData": "state", "bSortable": false},
                {"sWidth": "16%", "sTitle": "操作", "sClass": "center", "mData": "id", "bSortable": false}
            ],
            "aLengthMenu": [10, 30, 50],
            "aaSorting": [],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData["id"];
                var type = aData["type"];
                if (type == 2) {
                    $('td:eq(1)', nRow).html('<span class="label label-info">物业</span>');
                } else if (type == 3) {
                    $('td:eq(1)', nRow).html('<span class="label label-primary">商场</span>');
                } else {
                    $('td:eq(1)', nRow).html('<span class="label label-success">平台</span>');
                    $('td:eq(2)', nRow).html('--');
                }
                var gender = aData['gender'];
                $('td:eq(4)', nRow).html(gender == 1 ? '男' : '女');

                if (aData['created']) {
                    $('td:eq(6)', nRow).html(SysTool.Date.formatDate(aData['created'], 'yyyy-MM-dd hh:mm:ss'));
                }
                var state = aData['state'];
                if (state === 1) {
                    $('td:eq(7)', nRow).html('<span class="label label-success arrowed">正常</span>');
                } else {
                    $('td:eq(7)', nRow).html('<span class="label label-danger arrowed">禁用</span>');
                }
                // 操作
                var option = '<a title="管理管理员权限" href="/admins/' + id + '/roles" class="btn btn-xs btn-primary">权限</a>';
                option += '&nbsp;<a title="修改管理员信息" href="/admins/' + id + '/edit" class="btn btn-xs btn-info">编辑</a>';
                if (state === 1) {
                    option += '&nbsp;<a title="禁用管理员" userId="' + id + '" href="javascript:;" class="btn btn-xs btn-warning">禁用</a>';
                } else {
                    option += '&nbsp;<a title="启用管理员" userId="' + id + '" href="javascript:;" class="btn btn-xs btn-success">启用</a>';
                }
                $('td:eq(8)', nRow).html(option);
            }
            ,
            "fnServerParams":

                function (aoData) {
                    queryCondition("#search-form", aoData);
                }

            ,
            "fnStateLoaded":

                function (oSettings, oData) {
//                alert(JSON.stringify(oData));
                }
        });

        function queryCondition(queryFormId, aoData) {
            var queryParameters = $(queryFormId).serializeArray();
            $(queryParameters).each(function (i, v) {
                aoData.push(v);
            });
        }
    };

    UserMgr.prototype.refreshTable = function () {
        this.dataTable.dataTable().fnClearTable();
    };

    UserMgr.prototype.registEvent = function () {
        var that = this;
        that.$seachr_btn.click(function () {
            var oSettings = that.dataTable.dataTable().fnSettings();
            oSettings._iDisplayStart = 0;
            that.dataTable.dataTable().fnClearTable();
        });

        that.$btn_search_clear.click(function () {
            $('#username').val('');
            $('#realName').val('');
            $('#state').val('');
            that.$seachr_btn.click();
        });

        that.dataTable.on('click', 'a.btn-warning', function () {
            var $that = $(this);
            BootBox.confirm('确定禁用当前管理员？', function (b) {
                if (!b) return;
                var ids = $that.attr('userId');
                SysTool.Ajax.ajaxJsonCall('admins/' + ids + '/disable/', {}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });

        that.dataTable.on('click', 'a.btn-success', function () {
            var $that = $(this);
            BootBox.confirm('确定启用当前用户？', function (b) {
                if (!b) return;
                var ids = $that.attr('userId');
                SysTool.Ajax.ajaxJsonCall('admins/' + ids + '/enable/', {}, function (data) {
                    that.$seachr_btn.click();
                });
            });
        });

        $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
            //$(this).prev().focus();
        });

        SysTool.Html.bindEnterClick([], that.$seachr_btn);
    };

    window.UserMgr = new UserMgr();
});